<?php 
namespace Home\Controller;
use Think\Controller;
class GoodsController extends Controller{
	public function goods(){
		$goods = D('Goods');
		$goods_info = $goods->find(I('get.goods_id'));
		$goods_comment = $goods->relationGet('comment');
		if($goods_info){
			$this->history($goods_info);
		}
		$this->assign('mbx',$this->mbx($goods_info['cat_id']));
		$this->assign('goods_c',$goods_comment);
		$this->assign('goods',$goods_info);
		$this->display();
	}

	public function addcomment(){
		if(D('Comment')->add($_POST)){
			$this->success('评论添加成功');
		}
	}

	public function history($info){
		$row = session('?history')? session('history'): array();
		$g = array();

		$g['goods_name'] = $info['goods_name'];
		$g['shop_price'] = $info['shop_price'];
		$g['goods_id'] = $info['goods_id'];
		$row[$info['goods_id']] = $g;
		if(count($row) > 7){
			$key = key($row);
			unset($row[$key]);
		} 
		session('history',$row);
	}

	public function mbx($cat_id){
		$row = D('Cat')->find($cat_id);
		$tree[] = $row;

		while($row['parent_id'] > 0){
			$row = D('Cat')->find($row['parent_id']);
			$tree[] = $row;
		}
		return array_reverse($tree);
	}
}